package manager_service.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import manager_mapper.mapper.CourseMapper;
import manager_service.exception.CourseNotExistException;
import manager_service.service.BackCourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import test_common.entity.Course;
import test_common.entity.CourseExample;

import java.util.Date;
import java.util.List;

/**
 * @author zhangzheng
 * @create 2020/12/25
 */
@Service
@Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
public class BackCourseServiceImpl implements BackCourseService {

    @Autowired
    private CourseMapper courseMapper;

    @Override
    public void addCourse(Course course) {
        courseMapper.insertSelective(course);
    }

    @Override
    public PageInfo<Course> showCourseList(Integer pageNo,Integer pageSize) {
        PageHelper.startPage(pageNo,pageSize);
//        List<Course> list=courseMapper.selectAll((pageNo-1)*pageSize,pageSize);
        List<Course> list=courseMapper.selectByExample(new CourseExample());
        PageInfo<Course> pageHelper=new PageInfo<Course>(list);
        return pageHelper;
    }

    @Override
    public Course selectCourse(Integer id) throws CourseNotExistException {
        Course course= null;
        try {
            course = courseMapper.selectByPrimaryKey(id);
        } catch (Exception e) {
            e.printStackTrace();
            throw new CourseNotExistException("课程不存在");
        }
        return course;
    }

    @Override
    public void modifyCourse(Course course) {
        courseMapper.updateByPrimaryKeySelective(course);
    }

    @Override
    public void modifyCourseStatus(Course course) {
        courseMapper.updateByPrimaryKeySelective(course);
    }

    @Override
    public PageInfo<Course> searchCourses(String author, String courseName, String status, String courseTypeId, Date createDateStart, Date createDateEnd, Integer pageNoint, Integer pageSizeint) {

        PageHelper.startPage(pageNoint,pageSizeint);

        if (status.equals("-1")){
            status=null;
        }
        if (courseTypeId.equals("-1")){
            courseTypeId=null;
        }
        List<Course> list= courseMapper.searchCourses(author,courseName,status,courseTypeId,createDateStart,createDateEnd);
        PageInfo<Course> pageInfo=new PageInfo<>(list);
        return pageInfo;
    }
}
